package org.mercworks.knight.util;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

public class AllocationUtil
{
	private static final int SIZE_BYTE = 1;
	private static final int SIZE_FLOAT = 4;
	private static final int SIZE_INT = 4;

	/**
	 * Same function as in GLApp.java. Allocates a ByteBuffer to hold the given
	 * array of bytes.
	 * 
	 * @param bytearray
	 * @return ByteBuffer containing the contents of the byte array
	 */
	public static ByteBuffer allocBytes(byte[] bytearray)
	{
		ByteBuffer bb = ByteBuffer.allocateDirect(bytearray.length * SIZE_BYTE)
				.order(ByteOrder.nativeOrder());
		bb.put(bytearray).flip();
		return bb;
	}

	public static FloatBuffer allocFloats(float[] floatarray)
	{
		FloatBuffer fb = ByteBuffer.allocateDirect(floatarray.length * SIZE_FLOAT)
				.order(ByteOrder.nativeOrder()).asFloatBuffer();
		fb.put(floatarray).flip();
		return fb;
	}

	public static IntBuffer allocInts(int num)
	{
		return ByteBuffer.allocateDirect(num * SIZE_INT).order(
				ByteOrder.nativeOrder()).asIntBuffer();
	}

}
